package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.mapper.SysUserMapper;
import com.ktjiaoyu.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("sysUserService")
public class SysUserServiceImp implements SysUserService {
    // 定义 Mapper
    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    @Transactional
    public int saveBatch(List<SysUser> sysUserList) {
        int rows = 0;
        for (int i = 0; i < sysUserList.size(); i++) {
            // 拿出每一条用户数据插入
            SysUser sysUser = sysUserList.get(i);
            // 调用插入数据操作，计入总受影响行数
            rows += sysUserMapper.insert(sysUser);
        }
        // 返回总受影响行数
        return rows;
    }

    @Override
    @Transactional
    public int updateBatch(List<SysUser> sysUserList) {
        int rows = 0;
        for (int i = 0; i < sysUserList.size(); i++) {
            // 拿出每一条用户数据进行修改
            SysUser sysUser = sysUserList.get(i);
            // 调用修改数据操作，计入总受影响行数
            rows += sysUserMapper.update(sysUser);
        }
        // 返回总受影响行数
        return rows;
    }

    @Override
    @Transactional
    public int deleteBatch(List<Long> idList) {
        int rows = 0;
        for (int i = 0; i < idList.size(); i++) {
            // 拿出每个用户的ID进行删除
            Long id = idList.get(i);
            // 调用删除数据操作，计入总受影响行数
            rows += sysUserMapper.delete(id);
        }
        // 返回总受影响行数
        return rows;
    }


    // 提供 setter 方法用于注入属性
    public void setSysUserMapper(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }

    @Override
    public List<SysUser> findList(SysUser sysUser) {
        return sysUserMapper.getSysUserList(sysUser);
    }
}
